package com.study.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author hupengming
 * @ClassName
 * @Date 2022-10-19 14:27
 * @Version 1.0
 */
@Getter
@Configuration
public class CosConfig {

    @Value(value = "${cos.secretId:null}")
    private String secretId;
    @Value(value = "${cos.secretKey:null}")
    private String secretKey;
    @Value(value = "${cos.region:null}")
    private String region;

    @Value(value = "${cos.bucketName:null}")
    private String bucketName;

    @Value(value = "${cos.appid:null}")
    private String appid;


    @Bean
    public COSClient createCosClient(){
        // cos简单操作由cosClient发起请求，
        //设置用户信息，
        // secretId 和 secretKey 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
        COSCredentials credentials = new BasicCOSCredentials(secretId, secretKey);

        // ClientConfig 中包含了后续请求 COS 的客户端设置：
        ClientConfig clientConfig = new ClientConfig();
        //设置bucket的地域
        clientConfig.setRegion(new Region(region));

        /*设置请求协议
        clientConfig.setHttpProtocol(HttpProtocol.https);

        下列设置，可选
        设置socket读取超时,默认30s
        clientConfig.setSocketTimeout(30*1000);
        设置建立连接超时，默认30s
        clientConfig.setConnectionTimeout(30*1000);*/

        /*如果需要设置代理
        clientConfig.setHttpProxyIp("ip");
        clientConfig.setHttpProxyPort(80);*/

        return new COSClient(credentials,clientConfig);
    }

}
